<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="membermeal" :rules="rules" ref="memberMealFrom" label-width="150px">
      <el-form-item label="套餐名称：" prop="mealName">
        <el-input v-model="membermeal.mealName"></el-input>
      </el-form-item>
      <el-form-item label="套餐主图：" prop="pic">
        <single-upload v-model="membermeal.pic"></single-upload>
      </el-form-item>
      <el-form-item label="套餐价格：" prop="mealPrice">
        <el-input v-model="membermeal.mealPrice"></el-input>
      </el-form-item>
      <el-form-item label="套餐原价：" prop="mealPrice">
        <el-input v-model="membermeal.sourcePrice"></el-input>
      </el-form-item>
      <el-form-item label="套餐时长：" prop="mealExpiredate">
        <el-input v-model="membermeal.mealExpiredate"></el-input>
      </el-form-item>
      <el-form-item label="一级佣金比例：" prop="commissionRatio">
        <el-input v-model="membermeal.commissionRatio"></el-input>%
      </el-form-item>
      <el-form-item label="二级佣金比例：" prop="commissionRatio">
        <el-input v-model="membermeal.twoCommissionRatio"></el-input>%
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('memberMealFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('memberMealFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import {
  createMemberMeal,
  getMemberMeal,
  updateMemberMeal
} from "@/api/ums/membermeal";
import SingleUpload from '@/components/Upload/singleUpload'
import { formatDate } from "@/utils/date";
const defaultMemberMeal = {
  mealName: ""
};
export default {
  name: "MemberMealDetail",
  components:{SingleUpload},
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      membermeal: Object.assign({}, defaultMemberMeal),
      rules: {
        mealName: [
          { required: true, message: "请输入套餐名称", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur"
          }
        ],
        mealPrice: [
          {
            required: true,
            message: "请输入套餐价格",
            trigger: "blur"
          }
        ],
        mealExpiredate: [
          {
            required: true,
            message: "请输入套餐时长",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    if (this.isEdit) {
      getMemberMeal(this.$route.query.id).then(response => {
        this.membermeal = response.data;
      });
    } else {
      this.membermeal = Object.assign({}, defaultMemberMeal);
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            if (this.isEdit) {
              updateMemberMeal(this.$route.query.id, this.membermeal).then(
                response => {
                  if (response.code == 200) {
                    this.$refs[formName].resetFields();
                    this.$message({
                      message: "修改成功",
                      type: "success",
                      duration: 1000
                    });
                    this.$router.back();
                  } else {
                    this.$message({
                      message: response.msg,
                      type: "error",
                      duration: 1000
                    });
                  }
                }
              );
            } else {
              createMemberMeal(this.membermeal).then(response => {
                if (response.code == 200) {
                  this.$refs[formName].resetFields();
                  this.school = Object.assign({}, defaultMemberMeal);
                  this.$message({
                    message: "提交成功",
                    type: "success",
                    duration: 1000
                  });
                  this.$router.back();
                } else {
                  this.$message({
                    message: response.msg,
                    type: "error",
                    duration: 1000
                  });
                }
              });
            }
          });
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.membermeal = Object.assign({}, defaultMemberMeal);
    }
  }
};
</script>
<style>
</style>


